package com.kaiwukj.android.ufamily.mvp.ui.page.account.update;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.bean.SubImageBean;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.UpdateUserInfoEvent;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.UserInfoParams;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.listener.OnUploadImageListener;
import com.kaiwukj.android.ufamily.mvp.ui.page.account.AccountContract;
import com.kaiwukj.android.ufamily.utils.QiNiuUtil;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

public class UserUpdatePresenter extends BasePresenter<AccountContract.Model, AccountContract.EditView> {

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public UserUpdatePresenter(AccountContract.Model model, AccountContract.EditView view) {
        super(model, view);
    }

    public void update(UserInfoParams params) {
        mModel.updateUserInfo(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.hideLoading();
                        LogUtils.d("---------update success---------" + result);
                    }
                });
    }

    public void updateCommunity(int communityId) {
        mModel.updateCommunity(communityId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.hideLoading();
                        EventBus.getDefault().post(new UpdateUserInfoEvent(UpdateUserInfoEvent.REQUEST));
                    }
                });
    }

    public void updateAvatar(String filePath) {
        String fileName = String.format("avatar_%s.jpg", System.currentTimeMillis());
      /*  String fileName = "11.jpg";
        String token = "CGXFecxwFKD0FQtWrF8PhLN72nCC-tfGKzuXrs_j:v__UBNl2Md4S-UthmWTQ8JfqNIA=:eyJzY29wZSI6Imp5bC16ZzI6MTEuanBnIiwiZGVhZGxpbmUiOjE1OTI3OTQ5Mjd9";
        QiNiuUtil.getInstance().uploadImage(filePath, fileName, token, new OnUploadImageListener() {
            @Override
            public void onSuccess(SubImageBean imageBean) {
                LogUtils.d("onSuccess---------------->" + GsonUtils.toJson(imageBean));
                        *//*UserInfoParams params = new UserInfoParams();
                        params.setHeadImg(imageBean.getNormalUrl());
                        update(params);*//*
            }

            @Override
            public void onError(int errCode, String errMsg) {
                LogUtils.d("onError---------------->" + errCode + ":" + errMsg);
            }
        });*/
        mModel.getQNToken()
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<String>() {
                    @Override
                    public void onSuccess(@NonNull String result) {
                        QiNiuUtil.getInstance().uploadImage(filePath, fileName, result, new OnUploadImageListener() {
                            @Override
                            public void onSuccess(SubImageBean imageBean) {
                                UserInfoParams params = new UserInfoParams();
                                params.setHeadImg(imageBean.getNormalUrl());
                                update(params);
                            }

                            @Override
                            public void onError(int errCode, String errMsg) {

                            }
                        });
                    }
                });
    }

}
